package com.example.springbootwebrestfulcrud.config;

import com.example.springbootwebrestfulcrud.filter.MyFilter;
import com.example.springbootwebrestfulcrud.listener.MyListener;
import com.example.springbootwebrestfulcrud.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Auther: luzhanshi
 * @Date: 2021/2/27 - 02 - 27 - 15:19 
 * @Description: com.example.springbootwebrestfulcrud.config
 * @version: 1.0
 */

/**
 * 注册三大组件
 */
@Configuration
public class MyServletConfig {
    //注册servlet
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {


        return new ServletRegistrationBean(new MyServlet(), "/myServelet");
    }
    //注册Servlet过滤器

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServelet"));
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    //注册监听器
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<MyListener> servletRegistrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener());
        return servletRegistrationBean;
    }
}